package com.bolt.auth.security.admin.biz.sys.controller;

import com.bolt.auth.admin.api.sys.dto.DictDTO;
import com.bolt.auth.admin.api.sys.dto.converter.DictConverter;
import com.bolt.auth.admin.api.sys.entity.DictEntity;
import com.bolt.auth.security.admin.biz.sys.service.DictService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "系统：字典管理")
@RequestMapping("/api/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 数据字典列表分页
     *
     * @return
     */
    @GetMapping(value = "/page")
    @ApiOperation(notes = "数据字典列表分页数据字典列表分页", httpMethod = "GET", value = "数据字典列表分页")
    public ResultMessage list(Pageable pageable, DictDTO dto) {
        PageInfo <DictEntity> pageInfo = dictService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Results.success(pageInfo.map(K -> DictConverter.toDTO(K)));

    }


    @ApiOperation("查询字典")
    @GetMapping(value = "/all")
    public ResultMessage queryAll() {
        List <DictEntity> dicts = dictService.findAll();
        return Results.success(DictConverter.toDTO(dicts));
    }

    /**
     * 数据字典编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping
    @ApiOperation(notes = "数据字典编辑", httpMethod = "POST", value = "数据字典编辑")
    public ResultMessage <Void> saveOrUpdate(@Validated @RequestBody DictDTO dao) {
        dictService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 数据字典删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除数据字典", httpMethod = "POST", value = "根据ID删除数据字典")
    public ResultMessage <Void> doDelete(@RequestBody  Set<Long> ids) {
        for (Long id : ids) {
            dictService.deleteById(id);
        }
        return Results.success();
    }
}

 